home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gdevbgi.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  19KB  |  652 lines

  1. /* Copyright (C) 1989, 1990, 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevbgi.c */
  20. /* Ghostscript driver for Borland Graphics Interface (BGI) */
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <conio.h>
  24. #include <graphics.h>
  25. #include <mem.h>
  26. #include "gserrors.h"
  27. #include "gx.h"
  28. #include "gxdevice.h"
  29. #include "gxxfont.h"
  30.  
  31. #ifndef BGI_LIB                /* may be set in makefile */
  32. #  define BGI_LIB ""
  33. #endif
  34.  
  35. /*
  36.  * BGI supports these video cards:
  37.  *   Hercules, CGA, MCGA, EGA, VGA, AT&T 400, IBM 8514, PC3270.
  38.  * Highest resolution mode is used with all these video cards.
  39.  * EGA and VGA display 16 colors, the rest are black-and-white only.
  40.  * In addition, the environment variable BGIUSER may be used
  41.  * to define a user-supplied Super VGA driver: see the use.doc file
  42.  * for details.
  43.  */
  44. #define SUPER_VGA 999            /* bogus # for user-defined driver */
  45.  
  46. /* See gxdevice.h for the definitions of the procedures. */
  47.  
  48. dev_proc_open_device(bgi_open);
  49. dev_proc_close_device(bgi_close);
  50. dev_proc_map_rgb_color(bgi_map_rgb_color);
  51. dev_proc_map_color_rgb(bgi_map_color_rgb);
  52. dev_proc_fill_rectangle(bgi_fill_rectangle);
  53. dev_proc_tile_rectangle(bgi_tile_rectangle);
  54. dev_proc_copy_mono(bgi_copy_mono);
  55. dev_proc_copy_color(bgi_copy_color);
  56. dev_proc_draw_line(bgi_draw_line);
  57. dev_proc_get_xfont_procs(bgi_get_xfont_procs);
  58.  
  59. /* The device descriptor */
  60. typedef struct gx_device_bgi_s gx_device_bgi;
  61. struct gx_device_bgi_s {
  62.     gx_device_common;
  63.     int display_mode;
  64.     struct text_info text_mode;
  65. };
  66. #define bgi_dev ((gx_device_bgi *)dev)
  67. private gx_device_procs bgi_procs = {
  68.     bgi_open,
  69.     gx_default_get_initial_matrix,
  70.     gx_default_sync_output,
  71.     gx_default_output_page,
  72.     bgi_close,
  73.     bgi_map_rgb_color,
  74.     bgi_map_color_rgb,
  75.     bgi_fill_rectangle,
  76.     bgi_tile_rectangle,
  77.     bgi_copy_mono,
  78.     bgi_copy_color,
  79.     bgi_draw_line,
  80.     gx_default_get_bits,
  81.     gx_default_get_props,
  82.     gx_default_put_props,
  83.     NULL,
  84.     bgi_get_xfont_procs
  85. };
  86. gx_device_bgi gs_bgi_device = {
  87.     sizeof(gx_device_bgi),
  88.     &bgi_procs,
  89.     "bgi",
  90.     0, 0,        /* width and height are set in bgi_open */
  91.     1, 1,        /* density is set in bgi_open */
  92.     no_margins,
  93.     dci_black_and_white,
  94.     0        /* not open yet */
  95. };
  96.  
  97. /* Detection procedure for user-defined driver. */
  98. private int huge
  99. detectVGA(void)
  100. {    return gs_bgi_device.display_mode;
  101. }
  102.  
  103. /* Open the BGI driver for graphics mode */
  104. int
  105. bgi_open(gx_device *dev)
  106. {    int driver, mode;
  107.     char *bgi_user = getenv("BGIUSER");
  108.     char *bgi_path = getenv("BGIPATH");
  109.  
  110.     gettextinfo(&bgi_dev->text_mode);
  111.  
  112.     if ( bgi_path == NULL )
  113.         bgi_path = BGI_LIB;
  114.     if ( bgi_user != NULL )
  115.        {    /* A user-supplied driver is specified as "mode.dname", */
  116.         /* where mode is a hex number and dname is the name */
  117.         /* of the driver file. */
  118.         char dname[40];
  119.         if ( strlen(bgi_user) > sizeof(dname) ||
  120.              sscanf(bgi_user, "%x.%s", &mode, dname) != 2
  121.            )
  122.            {    eprintf("BGIUSER not in form nn.dname.\n");
  123.             exit(1);
  124.            }
  125.         gs_bgi_device.display_mode = mode;    /* sigh.... */
  126.         installuserdriver(dname, detectVGA);
  127.         driver = DETECT;
  128.         initgraph(&driver, &mode, bgi_path);
  129.         driver = SUPER_VGA;
  130.        }
  131.     else                /* not user-defined driver */
  132.        {    /* We include the CGA driver */
  133.         /* in the Ghostscript executable, so end-users don't */
  134.         /* have to have the BGI files. */
  135.         if ( registerfarbgidriver(CGA_driver_far) < 0 )
  136.            {    eprintf("BGI: Can't register CGA driver!\n");
  137.             exit(1);
  138.            }
  139.  
  140.         detectgraph(&driver, &mode);
  141.         if ( driver < 0 )
  142.            {    eprintf("BGI: No graphics hardware detected!\n");
  143.             exit(1);
  144.            }
  145.  
  146.         if ( driver == EGA64 )
  147.            {    /* Select 16 color video mode if video card is EGA with 64 Kb of memory */
  148.             mode = EGA64LO;
  149.            }
  150.  
  151.         /* Initialize graphics mode. */
  152.  
  153.         /* Following patch for AT&T 6300 is courtesy of */
  154.         /* Allan Wax, Xerox Corp. */
  155.         if ( driver == CGA )
  156.            {    /* The actual hardware might be an AT&T 6300. */
  157.             /* Try initializing it that way. */
  158.             int save_mode = mode;
  159.             driver = ATT400, mode = ATT400HI;
  160.             initgraph(&driver, &mode, bgi_path);
  161.             if ( graphresult() != grOk )
  162.                {    /* Nope, it was a real CGA. */
  163.                 closegraph();
  164.                 driver = CGA, mode = save_mode;
  165.                 initgraph(&driver, &mode, bgi_path);
  166.                }
  167.            }
  168.         else
  169.             initgraph(&driver, &mode, bgi_path);
  170.        }
  171.  
  172.        {    int code = graphresult();
  173.         if ( code != grOk )
  174.            {    eprintf1("Error initializing BGI driver: %s\n",
  175.                  grapherrormsg(code));
  176.             exit(1);
  177.            }
  178.        }
  179.  
  180.     /* Set parameters that were unknown before opening device */
  181.  
  182.     /* Size and nominal density of screen. */
  183.     /* The following algorithm maps an appropriate fraction of */
  184.     /* the display screen to an 8.5" x 11" coordinate space. */
  185.     /* This may or may not be what is desired! */
  186.     if ( dev->width == 0 )
  187.         dev->width = getmaxx() + 1;
  188.     if ( dev->height == 0 )
  189.         dev->height = getmaxy() + 1;
  190.     if ( dev->y_pixels_per_inch == 1 )
  191.        {    /* Get the aspect ratio from the driver. */
  192.         int arx, ary;
  193.         getaspectratio(&arx, &ary);
  194.         dev->y_pixels_per_inch = dev->height / 11.0;
  195.         dev->x_pixels_per_inch =
  196.             dev->y_pixels_per_inch * ((float)ary / arx);
  197.        }
  198.  
  199.     /* Find out if the device supports color */
  200.     /* (default initialization is monochrome). */
  201.     /* We only recognize 16-color devices right now. */
  202.     if ( getmaxcolor() > 1 )
  203.        {    static gx_device_color_info bgi_16color = dci_color(4, 2, 3);
  204.         dev->color_info = bgi_16color;
  205.        }
  206.     return 0;
  207. }
  208.  
  209. /* Close the BGI driver */
  210. int
  211. bgi_close(gx_device *dev)
  212. {    closegraph();
  213.     textmode(bgi_dev->text_mode.currmode);
  214.     return 0;
  215. }
  216.  
  217. /* Map a r-g-b color to the 16 colors available with an EGA/VGA video card. */
  218. gx_color_index
  219. bgi_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  220.   gx_color_value b)
  221. {    return (gx_color_index)
  222.         ((r > gx_max_color_value / 4 ? 4 : 0) +
  223.          (g > gx_max_color_value / 4 ? 2 : 0) +
  224.          (b > gx_max_color_value / 4 ? 1 : 0) +
  225.          (r > gx_max_color_value / 4 * 3 ||
  226.           g > gx_max_color_value / 4 * 3 ? 8 : 0));
  227. }
  228.  
  229. /* Map a color code to r-g-b.  Surprisingly enough, this is algorithmic. */
  230. int
  231. bgi_map_color_rgb(gx_device *dev, gx_color_index color,
  232.   gx_color_value prgb[3])
  233. {
  234. #define icolor (int)color
  235.     gx_color_value one =
  236.         (icolor & 8 ? gx_max_color_value : gx_max_color_value / 3);
  237.     prgb[0] = (icolor & 4 ? one : 0);
  238.     prgb[1] = (icolor & 2 ? one : 0);
  239.     prgb[2] = (icolor & 1 ? one : 0);
  240.     return 0;
  241. #undef icolor
  242. }
  243.  
  244. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  245. /* Color = gx_no_color_index means transparent (no effect on the image). */
  246. int
  247. bgi_copy_mono(gx_device *dev,
  248.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  249.   int x, int y, int w, int h,
  250.   gx_color_index zero, gx_color_index one)
  251. {    const byte *ptr_line = base + (sourcex >> 3);
  252.     int left_bit = 0x80 >> (sourcex & 7);
  253.     int dest_y = y, end_x = x + w;
  254.     int invert = 0;
  255.     int color;
  256.  
  257.     if ( zero == gx_no_color_index )
  258.        {    if ( one == gx_no_color_index ) return 0;
  259.         color = (int)one;
  260.        }
  261.     else
  262.        {    if ( one == gx_no_color_index )
  263.            {    color = (int)zero;
  264.             invert = -1;
  265.            }
  266.         else
  267.            {    /* Pre-clear the rectangle to zero */
  268.             setfillstyle(SOLID_FILL, (int)zero);
  269.             bar(x, y, x + w - 1, y + h - 1);
  270.             color = (int)one;
  271.            }
  272.        }
  273.  
  274.     while ( h-- )              /* for each line */
  275.        {    const byte *ptr_source = ptr_line;
  276.         register int dest_x = x;
  277.         register int bit = left_bit;
  278.         while ( dest_x < end_x )     /* for each bit in the line */
  279.            {    if ( (*ptr_source ^ invert) & bit )
  280.                 putpixel(dest_x, dest_y, color);
  281.             dest_x++;
  282.             if ( (bit >>= 1) == 0 )
  283.                 bit = 0x80, ptr_source++;
  284.            }
  285.         dest_y++;
  286.         ptr_line += raster;
  287.        }
  288.     return 0;
  289. }
  290.  
  291.  
  292. /* Copy a color pixel map.  This is just like a bitmap, except that */
  293. /* each pixel takes 4 bits instead of 1 when device driver has color. */
  294. int
  295. bgi_copy_color(gx_device *dev,
  296.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  297.   int x, int y, int w, int h)
  298. {    if ( gx_device_has_color(dev) )
  299.        {    /* color device, four bits per pixel */
  300.         const byte *line = base + (sourcex >> 1);
  301.         int dest_y = y, end_x = x + w;
  302.  
  303.         if ( w <= 0 ) return 0;
  304.         while ( h-- )              /* for each line */
  305.            {    const byte *source = line;
  306.             register int dest_x = x;
  307.             if ( sourcex & 1 )    /* odd nibble first */
  308.                {    int color =  *source++ & 0xf;
  309.                 putpixel(dest_x, dest_y, color);
  310.                 dest_x++;
  311.                }
  312.             /* Now do full bytes */
  313.             while ( dest_x < end_x )
  314.                {    int color = *source >> 4;
  315.                 putpixel(dest_x, dest_y, color);
  316.                 dest_x++;
  317.                 if ( dest_x < end_x )
  318.                    {    color =  *source++ & 0xf;
  319.                     putpixel(dest_x, dest_y, color);
  320.                     dest_x++;
  321.                    }
  322.                }
  323.             dest_y++;
  324.             line += raster;
  325.            }
  326.        }
  327.     else /* monochrome device: one bit per pixel */
  328.        {    /* bitmap is the same as bgi_copy_mono: one bit per pixel */
  329.         bgi_copy_mono(dev, base, sourcex, raster, id, x, y, w, h,
  330.             (gx_color_index)BLACK, (gx_color_index)WHITE);
  331.        }
  332.     return 0;
  333. }
  334.  
  335.  
  336. /* Fill a rectangle. */
  337. int
  338. bgi_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  339.   gx_color_index color)
  340. {    setfillstyle(SOLID_FILL, (int)color);
  341.     bar(x, y, x + w - 1, y + h - 1);
  342.     return 0;
  343. }
  344.  
  345.  
  346. /* Tile a rectangle.  If neither color is transparent, */
  347. /* pre-clear the rectangle to color0 and just tile with color1. */
  348. /* This is faster because of how bgi_copy_mono is implemented. */
  349. /* Note that this also does the right thing for colored tiles. */
  350. int
  351. bgi_tile_rectangle(gx_device *dev, const gx_bitmap *tile,
  352.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  353.   int px, int py)
  354. {    int tw = tile->size.x, th = tile->size.y;
  355.     int rw, rh, tx, ty;
  356.     int code;
  357.     byte image[4+4+256];
  358.     if ( w >> 1 < tw || h >> 1 < th ||
  359.          czero == gx_no_color_index || cone == gx_no_color_index ||
  360.          imagesize(x, y, x + tw - 1, y + th - 1) > sizeof(image)
  361.        )
  362.     {    if ( czero != gx_no_color_index && cone != gx_no_color_index )
  363.         {    bgi_fill_rectangle(dev, x, y, w, h, czero);
  364.             czero = gx_no_color_index;
  365.         }
  366.         return gx_default_tile_rectangle(dev, tile, x, y, w, h, czero, cone, px, py);
  367.     }
  368.     /* Handle edge strips.  We know w and h are both large. */
  369.     rh = h % th;
  370.     if ( rh )
  371.     {    code = gx_default_tile_rectangle(dev, tile, x, y + h - rh, w, rh, czero, cone, px, py);
  372.         if ( code < 0 ) return code;
  373.         h -= rh;
  374.     }
  375.     rw = w % tw;
  376.     if ( rw )
  377.     {    code = gx_default_tile_rectangle(dev, tile, x + w - rw, y, rw, h, czero, cone, px, py);
  378.         if ( code < 0 ) return code;
  379.         w -= rw;
  380.     }
  381.     /* Now w and h are multiples of tw and th respectively, */
  382.     /* and greater than 1.  Start by doing one tile the slow way. */
  383.     code = gx_default_tile_rectangle(dev, tile, x, y, tw, th, czero, cone, px, py);
  384.     if ( code < 0 ) return code;
  385.     /* Now replicate the tile. */
  386.     getimage(x, y, x + tw - 1, y + th - 1, &image[0]);
  387.     for ( ty = h; (ty -= th) >= 0; )
  388.     {    for ( tx = w; (tx -= tw) >= 0; )
  389.             putimage(x + tx, y + ty, &image[0], COPY_PUT);
  390.     }
  391.     return 0;
  392. }
  393.  
  394.  
  395. /* Draw a line */
  396. int
  397. bgi_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
  398.   gx_color_index color)
  399. {    setcolor((int)color);
  400.     setlinestyle(SOLID_LINE, 0, NORM_WIDTH);  /* solid, one pixel wide */
  401.     line(x0, y0, x1, y1);
  402.     return 0;
  403. }
  404.  
  405. /* ------ Platform font procedures ------ */
  406.  
  407. /*
  408.  * Note: Stroked BGI fonts lie about their height and baseline:
  409.  * the textheight is actually the position of the baseline,
  410.  * and the only way to find the actual height is to scan the bits.
  411.  */
  412.  
  413. /* Define xfont procedures. */
  414. private xfont_proc_lookup_font(bgi_lookup_font);
  415. private xfont_proc_char_xglyph(bgi_char_xglyph);
  416. private xfont_proc_char_metrics(bgi_char_metrics);
  417. private xfont_proc_render_char(bgi_render_char);
  418. private xfont_proc_release(bgi_release);
  419. private gx_xfont_procs bgi_xfont_procs = {
  420.     bgi_lookup_font,
  421.     bgi_char_xglyph,
  422.     bgi_char_metrics,
  423.     bgi_render_char,
  424.     bgi_release
  425. };
  426.  
  427. /* Return the xfont procedure record. */
  428. gx_xfont_procs *
  429. bgi_get_xfont_procs(gx_device *dev)
  430. {    return &bgi_xfont_procs;
  431. }
  432.  
  433. /* Define a BGI xfont. */
  434. typedef struct bgi_xfont_s {
  435.     gx_xfont_common common;
  436.     const char _ds *fname;
  437.     int index;            /* BGI font index */
  438.     gs_int_point ratio;        /* scaling ratio for character */
  439.     int base_size;            /* height of 4x 'A' */
  440.     int baseline;
  441. } bgi_xfont;
  442. private bgi_xfont all_fonts[] = {
  443.     { { &bgi_xfont_procs}, "Helvetica", SANS_SERIF_FONT },
  444.     { { &bgi_xfont_procs}, "Times-Roman", SIMPLEX_FONT },
  445.     { { &bgi_xfont_procs}, "Times-Bold", BOLD_FONT }
  446. };
  447.  
  448. /* Keep a record of a character temporarily rendered to the screen. */
  449. typedef struct char_image_s {
  450.     char str[2];
  451.     gs_int_point size;
  452.     uint image_size;
  453.     char *image;
  454. } char_image;
  455.  
  456. /* Forward references */
  457. private bgi_xfont *char_set_font(P1(gx_xfont *));
  458. private int char_set_image(P2(byte, char_image *));
  459. private void char_bbox(P3(bgi_xfont *, char_image *, gs_int_rect *));
  460. private void char_restore_image(P1(char_image *));
  461.  
  462. /* Look up a font. */
  463. gx_xfont *
  464. bgi_lookup_font(gx_device *dev, const byte *fname, uint len,
  465.   int encoding_index, const gs_uid *puid, const gs_matrix *pmat,
  466.   const gs_memory_procs *mprocs)
  467. {    float px, py;
  468.     int i;
  469.     if ( pmat->xy != 0 || pmat->yx != 0 || pmat->xx <= 0 || pmat->yy >= 0 )
  470.         return NULL;    /* unsupported transformation */
  471.     px = pmat->xx * 1000;
  472.     py = pmat->yy * -1000;
  473.     for ( i = 0; i < countof(all_fonts); i++ )
  474.     {    bgi_xfont *pf = &all_fonts[i];
  475.         if ( strlen(pf->fname) == len && !memcmp(pf->fname, fname, len) )
  476.         {    long rx, ry;
  477.             bgi_xfont *spf;
  478.             settextstyle(pf->index, HORIZ_DIR, 1);
  479.             if ( pf->base_size == 0 )    /* not set yet */
  480.             {    setusercharsize(1, 1, 1, 1);
  481.                 pf->base_size = textheight("A");
  482.             }
  483.             rx = px * 64 / pf->base_size;
  484.             ry = py * 64 / pf->base_size;
  485.             if ( rx <= 0 || ry <= 0 )
  486.                 return NULL;
  487.             spf = (bgi_xfont *)(*mprocs->alloc)(1,
  488.                     sizeof(bgi_xfont), "bgi_lookup_font");
  489.             if ( spf == 0 )
  490.                 return NULL;
  491.             *spf = *pf;
  492.             spf->ratio.x = rx;
  493.             spf->ratio.y = ry;
  494.             char_set_font((gx_xfont *)spf);
  495.             spf->baseline = textheight("A");  /* (see above) */
  496.             return (gx_xfont *)spf;
  497.         }
  498.     }
  499.     return NULL;        /* unsupported font name */
  500. }
  501.  
  502. /* Convert a character name or index to an xglyph code. */
  503. gx_xglyph
  504. bgi_char_xglyph(gx_xfont *xf, gs_char chr, int encoding_index,
  505.   gs_glyph glyph, gs_proc_glyph_name_t glyph_name_proc)
  506. {    if ( (encoding_index & ~1) != 0 ||  /* Standard & ISOLatin1 only */
  507.          chr < 32 || chr > 126    /* ASCII only */
  508.        )
  509.         return gx_no_xglyph;
  510.     return chr;
  511. }
  512.  
  513. /* Get the metrics for a character. */
  514. int
  515. bgi_char_metrics(gx_xfont *xf, gx_xglyph xg, int wmode,
  516.   gs_int_point *pwidth, gs_int_rect *pbbox)
  517. {    bgi_xfont *pf = char_set_font(xf);
  518.     char_image ci;
  519.     if ( wmode != 0 )
  520.         return gs_error_undefined;
  521.     if ( char_set_image((byte)xg, &ci) < 0 )
  522.         return gs_error_ioerror;
  523.     char_bbox(pf, &ci, pbbox);
  524.     pwidth->x = textwidth(ci.str);
  525.     pwidth->y = 0;
  526.     if ( pwidth->x == pbbox->q.x && pbbox->p.x == 0 )
  527.     {    /* This is a badly designed font with no inter-character */
  528.         /* spacing.  Add 1 pixel. */
  529.         pwidth->x++;
  530.     }
  531.     char_restore_image(&ci);
  532.     return 0;
  533. }
  534.  
  535. /* Render a character. */
  536. int
  537. bgi_render_char(gx_xfont *xf, gx_xglyph xg, gx_device *target,
  538.   int xo, int yo, gx_color_index color, int required)
  539. {    bgi_xfont *pf = char_set_font(xf);
  540.     char_image ci;
  541.     gs_int_rect bbox;
  542.     int xi, yi;
  543.     if ( target->dname == gs_bgi_device.dname )
  544.     {    /* Write directly to a BGI device. */
  545.         ci.str[0] = (char)xg;
  546.         ci.str[1] = 0;
  547.         setcolor((int)color);
  548.         outtextxy(xo, yo - pf->baseline, ci.str);
  549.         return 0;
  550.     }
  551.     if ( !required )
  552.         return gs_error_ioerror;    /* any error will do */
  553.     if ( char_set_image((byte)xg, &ci) < 0 )
  554.         return gs_error_ioerror;
  555.     char_bbox(pf, &ci, &bbox);
  556.     for ( yi = bbox.p.y; yi < bbox.q.y; yi++ )
  557.     {    for ( xi = bbox.p.x; xi < bbox.q.x; xi++ )
  558.           if ( getpixel(xi, yi + pf->baseline) != WHITE )
  559.         {    (*target->procs->fill_rectangle)(target,
  560.                 xi + xo, yi + yo, 1, 1, color);
  561.         }
  562.     }
  563.     char_restore_image(&ci);
  564.     return 0;
  565. }
  566.  
  567. /* Release an xfont. */
  568. private int
  569. bgi_release(gx_xfont *xf, const gs_memory_procs *mprocs)
  570. {    if ( mprocs != NULL )
  571.         (*mprocs->free)((char *)xf, 1, sizeof(bgi_xfont),
  572.                 "bgi_release");
  573.     return 0;
  574. }
  575.  
  576. /* ------ Font utilities ------ */
  577.  
  578. /* Set up a font. */
  579. private bgi_xfont *
  580. char_set_font(gx_xfont *xf)
  581. {    bgi_xfont *pf = (bgi_xfont *)xf;
  582.     settextstyle(pf->index, HORIZ_DIR, 0);
  583.     setusercharsize(pf->ratio.x, 64, pf->ratio.y, 64);
  584.     return pf;
  585. }
  586.  
  587. /* Write a character onto the screen. */
  588. private int
  589. char_set_image(byte ch, char_image *pci)
  590. {    char *str = pci->str;
  591.     int w, h;
  592.     uint size;
  593.     char *image;
  594.     str[0] = ch;
  595.     str[1] = 0;
  596.     w = textwidth(str);
  597.     h = textheight(str) << 1;    /* (see above) */
  598.     if ( w != 0 && h != 0 )
  599.     {    size = imagesize(0, 0, w - 1, h - 1);
  600.         image = malloc(size);
  601.         if ( image == 0 ) return -1;    /* allocation failed */
  602.         getimage(0, 0, w - 1, h - 1, image);
  603.         setfillstyle(SOLID_FILL, WHITE);
  604.         bar(0, 0, w - 1, h - 1);        /* clear */
  605.         setcolor(BLACK);
  606.         outtextxy(0, 0, str);
  607.     }
  608.     else
  609.     {    size = 0;
  610.         image = 0;
  611.     }
  612.     pci->size.x = w;
  613.     pci->size.y = h;
  614.     pci->image_size = size;
  615.     pci->image = image;
  616.     return 0;
  617. }
  618.  
  619. /* Find the bounding box of a character. */
  620. /* The character is already on the screen. */
  621. private void
  622. char_bbox(bgi_xfont *pf, char_image *pci, gs_int_rect *pbbox)
  623. {    int x0 = pci->size.x, y0 = pci->size.y, x1 = -1, y1 = 0;
  624.     int x, y;
  625.     int base = pf->baseline;
  626.     for ( y = pci->size.y; --y >= 0; )
  627.       for ( x = pci->size.x; --x >= 0; )
  628.         if ( getpixel(x, y) != WHITE )
  629.     {    if ( x < x0 ) x0 = x;
  630.         if ( x > x1 ) x1 = x;
  631.         if ( y < y0 ) y0 = y;
  632.         if ( y > y1 ) y1 = y;
  633.     }
  634.     if ( x0 > x1 )        /* blank */
  635.         pbbox->p.x = pbbox->q.x = pbbox->p.y = pbbox->q.y = 0;
  636.     else
  637.     {    pbbox->p.x = x0;
  638.         pbbox->p.y = y0 - base;
  639.         pbbox->q.x = x1 + 1;
  640.         pbbox->q.y = y1 + 1 - base;
  641.     }
  642. }
  643.  
  644. /* Restore the image under the character. */
  645. private void
  646. char_restore_image(char_image *pci)
  647. {    if ( pci->image != 0 )        /* might have been empty */
  648.     {    putimage(0, 0, pci->image, COPY_PUT);
  649.         free(pci->image);
  650.     }
  651. }
  652.